package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class SouSuoAddNumActivity extends BaseActivity implements View.OnClickListener{
    private String TAG ="addnumber";
    private ImageView addback;
    private TextView addtitle;
    private Button inworkbt;
    private Button sleepbt;
    private Button sellservice;
    private Button shopinstrall;
    private Button serviceinstrall;
    private Button cancelbt;
    private Button surebt;
    private SharedPreferences sharedPreferences;
    private String token;
    private String userId;
    private String appId;
    private RelativeLayout serviceMoneybt;
    private TextView storestate;
    private Switch addswitch;
    private String isOpenedStore;
    private String openStatus ;
    private String shopid;
    private String isShopOpen;
    private String id;
    private String isInsurance;
    private float xdown;
    private String auditStatus;
    private ImageView backmain;

    @Override
    protected void onStart() {
        super.onStart();
        getShopMessage();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sou_suo_add_num);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        shopid = sharedPreferences.getString("shopid", "");
        token = sharedPreferences.getString("token", "");
        userId = sharedPreferences.getString("userId", "");
        appId = sharedPreferences.getString("appid", "");
        Intent intent = getIntent();
        String isstate = intent.getStringExtra("isstate");


        isOpenedStore=isstate;
        getIsOpenStoreState();
        initAddView();

    }

        // 是否缴纳保证金
    private void getIsPayServiceMoney() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.PAYSERVICEMONEY);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                JSONObject object = JSON.parseObject(result);
                String data = object.getString("data");
                JSONObject object1 = JSON.parseObject(data);
                isInsurance = object1.getString("isInsurance");

                SharedPreferences.Editor edit = sharedPreferences.edit();
                edit.putString("isInsurance", isInsurance);
                edit.commit();
                if (isInsurance.equals("true")){
                    serviceMoneybt.setVisibility(View.GONE);
                }else {
                    serviceMoneybt.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(SouSuoAddNumActivity.this,ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
        // 是否已开店
    private void getIsOpenStoreState() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.ISOPENSTORE);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.d(TAG, "onSuccess: 是否开点--"+result);
                JSONObject jb = JSON.parseObject(result);
                String data = jb.getString("data");
                JSONObject object = JSON.parseObject(data);
                isOpenedStore = object.getString("isOpenedStore");
                isShopOpen = object.getString("isShopOpen");

                if (isOpenedStore.equals("true")){
                    getIsPayServiceMoney();
                    getShopMessage();
                }else {
                    ToastWindow(0);
                }
                if (isShopOpen==null){// 营业休息


                }else {
                    if (isShopOpen.equals("true")){
                        addswitch.setChecked(true);
                        storestate.setText("营业中");
                    }else {
                        addswitch.setChecked(false);
                        storestate.setText("休息中");
                    }

                    addswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                            if (isChecked==true){

                                storestate.setText("营业中");
                                openStatus = "1";
                                toWindow(openStatus);
                            }else {

                                storestate.setText("休息中");
                                openStatus = "0";
                                toWindow(openStatus);

                            }
                        }
                    });
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d(TAG, "onError: 是否开店"+ex);
                ErrType.errType(SouSuoAddNumActivity.this,ex);

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    // 初始化 营业 休息店铺装修 发布任务 服务编辑按钮
    private void initAddView() {
        addswitch = (Switch) findViewById(R.id.fw_addno_switch);
        addback = (ImageView) findViewById(R.id.mine_add_back);
        addtitle = (TextView) findViewById(R.id.sousuo_addnum_title);
        sellservice = (Button) findViewById(R.id.fw_addno_sellservice);
        shopinstrall = (Button) findViewById(R.id.fw_addno_shopinstrall);
        serviceinstrall = (Button) findViewById(R.id.fw_addno_serviceinstrall);
        serviceMoneybt = (RelativeLayout) findViewById(R.id.addnumber_servicemoneybt);
        storestate = (TextView) findViewById(R.id.add_storestate);
        backmain = (ImageView) findViewById(R.id.souadd_backmain);


        // 设置点击监听
        serviceMoneybt.setOnClickListener(this);
        addback.setOnClickListener(this);
        sellservice.setOnClickListener(this);
        shopinstrall.setOnClickListener(this);
        serviceinstrall.setOnClickListener(this);
        backmain.setOnClickListener(this);
       // addswitch.setOnClickListener(this);
    }
        // 店铺状态弹窗
    private void toWindow(String openStatus) {
        AlertDialog.Builder builder = new AlertDialog.Builder(SouSuoAddNumActivity.this);
        View view = LayoutInflater.from(SouSuoAddNumActivity.this).inflate( R.layout.cancel_servicedialog, null);
        builder.setView(view);
        TextView t2 = (TextView) view.findViewById(R.id.cancel_t2);
        Button canbt = (Button) view.findViewById(R.id.cancel_cancelbt);
        Button surebt = (Button) view.findViewById(R.id.cancel_surebt);
        if (openStatus.equals("1")){
            t2.setText("恭喜营业！开门大吉 祝您财源滚滚");
        }else {
            t2.setText("你确定要暂时关闭店铺,暂时休息一下吗？");
        }
        final AlertDialog alertDialog = builder.create();
        canbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        surebt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                inStrallStoreState();
                alertDialog.dismiss();
            }
        });
        alertDialog.show();


    }

    // 点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.souadd_backmain:
                startActivity(new Intent(SouSuoAddNumActivity.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
            // 点击调转至详情页
            case R.id.mine_add_back:
                finish();
                break;
            case R.id.addnumber_servicemoneybt:
                startActivity(new Intent(SouSuoAddNumActivity.this,MineMoney.class));
                break;
            case R.id.fw_addno_sellservice:
                if (isOpenedStore.equals("true")){
                    Intent i = new Intent(SouSuoAddNumActivity.this,FWSellServiceActivity.class);
                    i.putExtra("from","open");
                    startActivity(i);
                }else {
                    ToastWindow(0);
                }
                break;
            case R.id.fw_addno_shopinstrall:
                if (auditStatus!=null){
                    if (auditStatus.equals("1")){// 店铺审核通过
                        Intent i = new Intent(SouSuoAddNumActivity.this,FWOpenShopActivity.class);
                        i.putExtra("isOpenedStore",isOpenedStore);
                        i.putExtra("isInsurance",isInsurance);
                        startActivity(i);
                    }else if (auditStatus.equals("0")){// 店铺审核中
                        Toast.makeText(this, "店铺审核中", Toast.LENGTH_SHORT).show();
                    }else if (auditStatus.equals("-1")){// 店铺审核未通过
                        ToastWindow(1);
                    }
                }else {
                    ToastWindow(0);
                }
                break;
            case R.id.fw_addno_serviceinstrall:
                // 编辑服务
                if (isOpenedStore.equals("true")){
                    Intent i = new Intent(SouSuoAddNumActivity.this,FWServiceInstrallActivity.class);
                    startActivity(i);
                }else {
                    ToastWindow(0);
                }
                break;
        }
    }
        // 设置店铺状态
    private void inStrallStoreState() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.INSTRALLSTORESTATE);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        JSONObject object = new JSONObject();

        object.put("shopId",shopid);
        object.put("openStatus",openStatus);
        params.setBodyContent(object.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    // 获取店铺信息
    private void getShopMessage() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.GETSTOREMESSAGE);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        Log.d(TAG, "getShopMessage:加号--店铺信息--"+token+"//"+appId+"//"+userId);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.d(TAG, "onSuccess: 获取店铺信息--11-"+result);
                JSONObject object = JSON.parseObject(result);
                String data = object.getString("data");
                String status = object.getString("status");
                if (status.equals("1")){
                    if (data==null){

                    }else {
                        JSONObject object1 = JSON.parseObject(data);
                        id = object1.getString("id");
                        auditStatus = object1.getString("auditStatus");
                        SharedPreferences.Editor edit = sharedPreferences.edit();
                        if (id!=null){
                            edit.putString("shopid",id);
                            edit.commit();
                        }else {}

                    }
                }else {}


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(SouSuoAddNumActivity.this,ex);
                Log.d(TAG, "onError: 获取店铺信息-11--"+ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    /**
     *  开店提示弹窗
     * @param stateShop 店铺状态 0 未开店 提示开店
     *                          1 已开店 提示资料审核未通过
     *
     */
    public void ToastWindow(int stateShop){
        AlertDialog.Builder builder = new AlertDialog.Builder(SouSuoAddNumActivity.this);
        View dialog1 = LayoutInflater.from(SouSuoAddNumActivity.this).inflate(R.layout.custom_dislog_1, null);
        builder.setView(dialog1);
        TextView message = (TextView) dialog1.findViewById(R.id.custom_message);
        cancelbt = (Button) dialog1.findViewById(R.id.custom_dialog_cancelbt);
        surebt = (Button) dialog1.findViewById(R.id.custom_dialog_surebt);
        cancelbt.setText("取消");
        switch (stateShop){
            case 0:
                surebt.setText("马上开店");
                message.setText("您还未开通属于你自己的店铺 点击马上开店");
                break;
            case 1:
                surebt.setText("确定");
                message.setText("您的店铺资料审核未通过,请重新填写");
                break;
        }

        final AlertDialog dialog = builder.create();
        cancelbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        surebt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(SouSuoAddNumActivity.this,FWOpenShopActivity.class);
                intent.putExtra("isOpenedStore",isOpenedStore);
                startActivity(intent);
                dialog.dismiss();
            }
        });
        dialog.show();
    }


}
